﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace August.UI
{
    public class Line: ShapeBase
    {
        public GraphicsPath ScaleGraphicPath = new GraphicsPath();
        public Line(PointF[] pointFs)
        {
            PointFs = new List<PointF>(pointFs).ToArray(); ;
        }

        internal override void OnWheelPaint(PaintEventArgs e)
        {
            Line line = this;
            PointF[] m_pointFs = new PointF[line.PointFs.Length];
            line.PointFs.CopyTo(m_pointFs, 0);
            using (System.Drawing.Drawing2D.Matrix transform = new Matrix())
            {
                transform.Scale(Parent.Owner.ScaleX, Parent.Owner.ScaleY, System.Drawing.Drawing2D.MatrixOrder.Append);
                transform.Translate(Parent.Owner.OffsetX, Parent.Owner.OffsetY, System.Drawing.Drawing2D.MatrixOrder.Append);
                transform.TransformPoints(m_pointFs);
                line.TransForm = transform.Clone();
                line.LastPointFs = m_pointFs;

            }
            GraphicsPath graphicsPath = new GraphicsPath();
            graphicsPath.AddLines(m_pointFs);
            e.Graphics.DrawLines(new Pen(line.m_DrawColor, line.DrawLineWidth), m_pointFs);
            line.DrawRegion = new Region(graphicsPath);
            line.DrawGraphicsPath = graphicsPath;
            line.MoveGraphicPath = graphicsPath;
            line.ScaleGraphicPath = graphicsPath;
            base.OnWheelPaint(e);
        }

        internal override bool OnDownPaint(MouseEventArgs e)
        {
            if (this.DrawGraphicsPath.IsOutlineVisible(e.X, e.Y, new Pen(Color.Empty, DrawLineWidth + 6)))
            {
                m_isSelected = true;
                m_DrawColor = ShapeBase.ColorMouseDown;
                m_mouseDownMovePointFs = new PointF[PointFs.Length];
                PointFs.CopyTo(m_mouseDownMovePointFs, 0);
                int index = -1;
                m_IsSelectedPoint = IsHitPoint(e.X, e.Y, LastPointFs, ref index);
                m_selectedPointIndex = index;
                return true;
            }
            else
                return false;
        }

        internal override void OnMovePaint(MouseEventArgs e)
        {
            base.OnMovePaint(e);
            if (m_isSelected && IsUserShape)
            {

                if (m_IsSelectedPoint)
                {
                    Point point = e.Location;
                    Matrix matrix = new Matrix();
                    matrix.Translate((-m_mouseDownPointF.X + point.X) * 1f / Parent.Owner.ScaleX, (-m_mouseDownPointF.Y + point.Y) * 1f / Parent.Owner.ScaleY);
                    m_mouseDownMovePointFs.CopyTo(PointFs, 0);
                    PointF[] pointFsOneSelect = new PointF[1] { PointFs[m_selectedPointIndex] };
                    matrix.TransformPoints(pointFsOneSelect);
                    PointFs[m_selectedPointIndex] = pointFsOneSelect[0];
                }
                else
                {
                    Point point = e.Location;
                    Matrix matrix = new Matrix();
                    matrix.Translate((-m_mouseDownPointF.X + point.X) * 1f / Parent.Owner.ScaleX, (-m_mouseDownPointF.Y + point.Y) * 1f / Parent.Owner.ScaleY);
                    m_mouseDownMovePointFs.CopyTo(PointFs, 0);
                    matrix.TransformPoints(PointFs);
                }
            }
        }
        protected bool IsHitPoint(int x, int y, PointF[] pointFs, ref int index)
        {
            index = -1;
            for (int k = 0; k < pointFs.Length; k++)
            {
                if (Math.Abs(pointFs[k].X - x) < 6 && Math.Abs(pointFs[k].Y - y) < 6)
                {
                    index = k;
                    return true;
                }
            }
            return false;
        }


    }
}
